package com.sskj.fca.ui.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.fca.R;
import com.sskj.fca.bean.VcerifyBean;
import com.sskj.fca.presenter.CheckoutPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by QinKang on 2019/1/1.
 */

@Route(path = RConfig.APP_CHECKOUT)//开奖校验
public class CheckoutActivity extends BaseActivity<CheckoutPresenter> {
    @BindView(R.id.iv_checkout_back)
    ImageView ivCheckoutBack;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @Autowired
    String boomHash;
    @BindView(R.id.tv_boom_hash)
    EditText tvBoomHash;
    @BindView(R.id.et_hash_count)
    EditText etHashCount;
    @BindView(R.id.btn_check)
    Button btnCheck;
    private SlimAdapter slimAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_checkout;
    }

    @Override
    public CheckoutPresenter getPresenter() {
        return new CheckoutPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        ClickUtil.click(ivCheckoutBack, () -> {
            finish();
        });
        ClickUtil.click(btnCheck, () -> {
            if (TextUtils.isEmpty(tvBoomHash.getText().toString())) {
                ToastUtil.showShort("请输入校验码");
                return;
            }
            if (TextUtils.isEmpty(etHashCount.getText().toString())) {
                ToastUtil.showShort("请输入校验个数");
                return;
            }
            mPresenter.toVcerify(tvBoomHash.getText().toString().trim(), etHashCount.getText().toString().trim());
        });
        if (!TextUtils.isEmpty(boomHash)) {
            tvBoomHash.setText(boomHash);
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_checkout, new SlimInjector<VcerifyBean>() {
            @Override
            public void onInject(VcerifyBean data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.background(R.id.bg_checkout, ContextCompat.getDrawable(App.INSTANCE, R.drawable.app_bg_7d7));
                } else {
                    injector.backgroundColor(R.id.bg_checkout, ContextCompat.getColor(App.INSTANCE, R.color.app_title));
                }
                injector.text(R.id.tv_boomHash, data.getBoomHash())
                        .text(R.id.tv_boomValue, data.getBoomValue());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
//        Flowable.rangeLong(0, 10)
//                .map(i -> i + "")
//                .toList()
//                .subscribe(strings -> slimAdapter.updateData(strings));
    }

    public void updataUI(List<VcerifyBean> data) {
        slimAdapter.updateData(data);
    }

}
